package com.ndp.fb.walle.controller;

import com.alibaba.fastjson.JSON;
import com.ndp.fb.enums.ErrorType;
import com.ndp.fb.exception.BusinessException;
import com.ndp.fb.rdb.model.TargetingSpecTemplate;
import com.ndp.fb.rdb.model.User;
import com.ndp.fb.walle.business.TargetingSpecTemplateService;
import com.ndp.fb.walle.model.vo.OutputResult;
import com.ndp.fb.walle.model.vo.targetingspectemplate.param.TargetingSpecTemplateParam;
import com.ndp.fb.walle.model.vo.targetingspectemplate.param.TargetingSpecTemplateQueryParam;
import com.ndp.fb.walle.util.ResponseUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/3/25.
 */
@RequestMapping("/targetingspectemplates")
@Controller
public class TargetingSpecTemplateController {
    private final org.slf4j.Logger LOG = LoggerFactory.getLogger(getClass());

    @Autowired
    private TargetingSpecTemplateService targetingSpecTemplateService;

    /**
     * 创建兴趣词模板
     * @param param              param
     * @param httpServletRequest httpServletRequest
     * @return ResponseEntity<String>
     * @author Sisi 创建时间 2014年7月21日 下午2:36:31
     */
    @RequestMapping(value = "createTargetingSpecTemplate")
    @ResponseBody
    public ResponseEntity<String> createTargetingSpecTemplate(
            @RequestParam(value = "param", required = true) String param, HttpServletRequest httpServletRequest) {
        ErrorType errorType = null;
        Map<String, Object> data = null;
        try {
            TargetingSpecTemplateParam targetingSpecTemplateParam = JSON.parseObject(param,
                    TargetingSpecTemplateParam.class);
            User user = (User) httpServletRequest.getSession().getAttribute("user");
            targetingSpecTemplateParam.setUserId(user.getTicket());
            targetingSpecTemplateParam.setOrgId(user.getOrgId());
            Long id = targetingSpecTemplateService.createTargetingSpecTemplate(targetingSpecTemplateParam);
            data = new HashMap<>();
            data.put("id", id);
        } catch (BusinessException businessException) {
            errorType = businessException.getErrorType();
            LOG.error(businessException.getMessage(), businessException);
        } catch (Exception e) {
            errorType = ErrorType.INTERNAL_EXCEPTION;
            LOG.error(e.getMessage());
        }
        OutputResult outputResult = ResponseUtils.bulidOutputResult(errorType, data);
        return ResponseUtils.returnJsonWithUTF8(JSON.toJSONString(outputResult));
    }

    /**
     * 获取兴趣词模板
     * @param param param
     * @return ResponseEntity<String>
     * @author Sisi
     * 创建时间 2014年7月21日 下午2:53:49
     */
    @RequestMapping(value = "getTargetingSpecTemplatesByType")
    @ResponseBody
    public ResponseEntity<String> getTargetingSpecTemplatesByType(
            @RequestParam(value = "param", required = true) String param, HttpServletRequest request) {
        ErrorType errorType = null;
        List<TargetingSpecTemplate> data = null;
        TargetingSpecTemplateQueryParam queryParam = JSON.parseObject(param, TargetingSpecTemplateQueryParam.class);
        try {
            User user = (User) request.getSession().getAttribute("user");
            data = targetingSpecTemplateService.findAllTargetingSpecTemplates(user.getOrgId(), queryParam.getFbConnectObjectId(),
                    queryParam.getProductConfigurationId());
        } catch (Exception e) {
            errorType = ErrorType.INTERNAL_EXCEPTION;
            LOG.error(e.getMessage());
        }
        OutputResult outputResult = ResponseUtils.bulidOutputResult(errorType, data);
        return ResponseUtils.returnJsonWithUTF8(JSON.toJSONString(outputResult));
    }
}
